<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({ expectedData, actualData } = {}) {
      this.chart.setOption({
        color: ["#57aeff", "#ffe374"],
        backgroundColor:"transparent",
        xAxis: {
          data: ["党委会", "中心组学习", "党员大会", "支部委员会", "主题党日", "民主生活会", "民主评议党员","其他","党小组会","党课","组织生活会"],
          axisTick: {
            // show: false,
            alignWithLabel:true
          },
          axisLabel:{
            color:'#666666'
          },
          axisLine:{
            show:true,
            lineStyle:{
              color:'#999999'
            }
          }
        },
        grid: {
          left: 20,
          bottom: 0,
          right:0,
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
          backgroundColor:'rgba(255,255,255,0.8)',
          textStyle:{
            color:'#666666'
          },
          padding: [10, 10],
        },
        yAxis: {
          axisTick: {
            show: false,
          },
          axisLabel:{
            color:'#666666'
          },
          splitLine:{
            lineStyle:{
              type:'dashed'
            }
          },
          splitArea:{
            show: false
          }
        },
        legend: {
          icon: "circle",
          itemWidth: 8,
          itemHeight: 8,
          data: ["线下会议", "直播会议"],
          textStyle: {
            color: "#999999",
          },
        },
        series: [
          {
            name: "线下会议",
            barWidth:22,
            type: "bar",
            barGap:0,
            data: expectedData
          },
          {
            name: "直播会议",
            type: "bar",
            barWidth:22,
            data: actualData
          },
        ],
      });
    },
  },
};
</script>
